using Microsoft.Extensions.Logging;
using Sage.Http.Authentication;
using Sage.Http.Models;

namespace Sage.Http.Core
{
    /// <summary>
    /// HTTP请求工厂，专门负责创建和构建HTTP请求
    /// </summary>
    /// <remarks>
    /// 初始化HttpRequestFactory实例
    /// </remarks>
    /// <param name="logger">日志记录器（可选）</param>
    [Obsolete("HttpRequestFactory已过时，请使用HttpRequestBuilderFactory代替。此类将在未来版本中移除。", false)]
    public class HttpRequestFactory(ILogger? logger = null)
    {
        private readonly HttpRequestBuilderFactory _builderFactory = new(logger);

        /// <summary>
        /// 设置默认认证提供者，将应用于所有请求
        /// </summary>
        /// <param name="authProvider">认证提供者</param>
        public void SetDefaultAuthentication(IAuthenticationProvider authProvider)
        {
            ArgumentNullException.ThrowIfNull(authProvider);
            _builderFactory.SetDefaultAuthentication(authProvider);
        }

        /// <summary>
        /// 创建HTTP请求构建器
        /// </summary>
        /// <param name="httpRequestManager">HTTP请求管理器</param>
        /// <param name="method">HTTP方法</param>
        /// <param name="url">请求URL</param>
        /// <returns>HTTP请求构建器</returns>
        public HttpRequestBuilder CreateRequest(HttpRequestManager httpRequestManager, HttpMethod method, string url)
        {
            return _builderFactory.CreateRequest(httpRequestManager, method, url);
        }

        /// <summary>
        /// 创建HTTP请求构建器，使用HttpRouteInfo对象指定API信息
        /// </summary>
        /// <param name="httpRequestManager">HTTP请求管理器</param>
        /// <param name="routeInfo">API路由信息</param>
        /// <returns>HTTP请求构建器</returns>
        /// <remarks>
        /// 如果<paramref name="routeInfo"/>的<see cref="HttpRouteInfo.UrlTemplate"/>包含格式化占位符（如"/api/users/{0}/orders/{1}"），
        /// 请确保在调用此方法前先调用<see cref="HttpRouteInfo.BuildUrl"/>方法提供实际参数，否则将使用未格式化的模板URL。
        /// </remarks>
        public HttpRequestBuilder CreateRequest(HttpRequestManager httpRequestManager, HttpRouteInfo routeInfo)
        {
            return _builderFactory.CreateRequest(httpRequestManager, routeInfo);
        }

        /// <summary>
        /// 构建包含查询参数的请求 URL
        /// </summary>
        /// <param name="url">基础 URL</param>
        /// <param name="queryParams">查询参数字典</param>
        /// <returns>带有查询参数的完整 URL</returns>
        public static string BuildRequestUrl(string url, Dictionary<string, object>? queryParams)
        {
            return HttpRequestBuilderFactory.BuildRequestUrl(url, queryParams);
        }

        /// <summary>
        /// 添加请求头
        /// </summary>
        /// <param name="request">HTTP请求消息</param>
        /// <param name="headers">头部集合</param>
        public static void AddHeaders(HttpRequestMessage request, Dictionary<string, object>? headers)
        {
            HttpRequestBuilderFactory.AddHeaders(request, headers);
        }

        /// <summary>
        /// 设置请求超时
        /// </summary>
        /// <param name="request">HTTP请求消息</param>
        /// <param name="timeout">超时时间</param>
        public static void SetRequestTimeout(HttpRequestMessage request, TimeSpan timeout)
        {
            HttpRequestBuilderFactory.SetRequestTimeout(request, timeout);
        }
    }
}